<?php
namespace My\Controller;

use Utuo\BackgroundController;

class GrouponGoodsController extends BackgroundController {
	
	private $gid = null;
	
	private $status = array(
		'0' => '禁用',
		'1' => '启用',
	);

	public function _initialize() {
		$this->gid = I('gid', 0, "intval");
		$this->setControllerTitle('团购商品管理');
		$this->setActionTitle(array(
			'add' => '商品添加',
			'modify' => '商品编辑',
		), '', 'gid='.$this->gid);
		$this->setActionNav(false);
		$this->assign("gid", $this->gid);
	}
	
	/**
	 * 添加商品
	 */
	public function addAction() {
		$this->display('save');
	}
	
	/**
	 * 提交保存
	 */
	public function saveAction() {
		$d = D("GrouponGoods");
		if ( $d->create() ) {
			if (empty($_REQUEST['id'])) {
				unset($d->id);
				if ($lastId = $d->add()) {
					if ( $this->gid ) {
						M("GrouponGoodson")->add(array(
							'cid' => $this->aid,
							'groupon_id' => $this->gid,
							'goods_id' => $lastId,
							'limit_buy' => 0,
						));
					}
					$this->ajaxSuccess("添加成功！");
				} else {
					$this->ajaxError("添加失败！".$d->getError());
				}
			} else {
				if (false !== $d->save()) {
					$this->ajaxSuccess("修改成功！");
				} else {
					$this->ajaxError("修改失败！");
				}
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
}